local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/`dataset'.dta", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_b2* *_b3*
drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2_`var' = `var'2 - ($sdv * `var'2se)
	gen highs2_`var' = `var'2 + ($sdv * `var'2se)
	gen lows3_`var' = `var'3 - ($sdv * `var'3se)
	gen highs3_`var' = `var'3 + ($sdv * `var'3se)
}

		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		sencode variables, gen(vars) gsort(ordering)
		gen vars1 = vars+.3
		gen vars2 = vars+.15
		twoway	(scatter y2 vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2_y highs2_y vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2_y highs2_y vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter y3 vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3_y highs3_y vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3_y highs3_y vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iy2 vars1, mcolor(red) msymbol(square)) ///
				(rcap lows2_Iy highs2_Iy vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iy highs2_Iy vars1 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2" 7 "Input Access") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) 
				
			graph export "$figures/`samplenamefile'_y_2O1I.png", as(png) replace width(1800) height(1000)

		twoway	(scatter yn2 vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2_yn highs2_yn vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2_yn highs2_yn vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter yn3 vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3_yn highs3_yn vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3_yn highs3_yn vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iyn2 vars1, mcolor(red) msymbol(square)) ///
				(rcap lows2_Iyn highs2_Iyn vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iyn highs2_Iyn vars1 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2" 7 "Input Access") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) 
				
			graph export "$figures/`samplenamefile'_yn_2O1I.png", as(png) replace width(1800) height(1000)	

		twoway	(scatter ycn2 vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2_ycn highs2_ycn vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2_ycn highs2_ycn vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter ycn3 vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3_ycn highs3_ycn vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3_ycn highs3_ycn vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color)
				
				
			graph export "$figures/`samplenamefile'_ycn_2O1I.png", as(png) replace width(1800) height(1000)

